package cos.android.voleysplash.element;

import java.util.Vector;

import cos.android.voleysplash.level.Point;



public class Polygon {

	Vector<Point> aristasTemplate;

	public Polygon(Vector<Point> aristasTemplate) {
		super();
		this.aristasTemplate = aristasTemplate;
	}

	public void setAristasTemplate(Vector<Point> aristas) {
		this.aristasTemplate = aristas;
	}

	public Vector<Point> getAristas() {
		return aristasTemplate;
	}

	public Vector<Point> getRealAristas(Point origin) {

		Vector<Point> real = new Vector<Point>();
		for (Point point : aristasTemplate) {
			Point newPoint = new Point(point.getxPos() + origin.getxPos(),
					point.getyPos() + origin.getyPos());
			real.add(newPoint);
		}

		return real;
	}

	public boolean collition(Point origin, Point point) {

		Vector<Point> aristasReal = getRealAristas(origin);
		int cn = 0; // the crossing number counter
		// loop through all edges of the polygon
		for (int i = 0; i < aristasReal.size() - 1; i++) { // edge from V[i]
															// to V[i+1]

			if (((aristasReal.elementAt(i).getyPos() <= point.getyPos()) && (aristasReal
					.elementAt(i + 1).getyPos() > point.getyPos())) // an upward
																	// crossing
					|| ((aristasReal.elementAt(i).getyPos() > point.getyPos()) && (aristasReal
							.elementAt(i + 1).getyPos() <= point.getyPos()))) { // a
																				// downward
				// crossing
				// compute the actual edge-ray intersect x-coordinate
				float vt = (float) (point.getyPos() - aristasReal.elementAt(i)
						.getyPos())
						/ (aristasReal.elementAt(i + 1).getyPos() - aristasReal
								.elementAt(i).getyPos());
				if (point.getxPos() < aristasReal.elementAt(i).getxPos()
						+ vt
						* (aristasReal.elementAt(i + 1).getxPos() - aristasReal
								.elementAt(i).getxPos())) // P.x <
					// intersect
					++cn; // a valid crossing of y=P.y right of P.x
			}
		}
		if (cn % 2 == 0) {
			/*
			 * System.out.println("Rectangulo"); for (int i = 0; i <
			 * aristasReal.size(); i++) {
			 * 
			 * System.out.println(aristasReal.elementAt(i).toString()); }
			 * System.out.println("");
			 */
			return false;
		} else {
			return true;
		}
		// 0 if even (out), and 1 if odd (in)
	}

	public Point collition(Point origin, Element element) {
		// Vector<Point> aristasReal = getRealAristas(origin);

		if (element.isCircle()) {

			/*
			 * for (Point point : element.getAristas()) { if (collition(origin,
			 * point)) { System.out.println("colision!!!"); //return (new
			 * Point(3, 3)); } }
			 */
			/*
			 * for (int i = 0; i < aristasReal.size(); i++) { Point point =
			 * aristasReal.elementAt(i); double distance =
			 * Motor.circlePointDistance(element, point); if (distance <=
			 * element.getRadius()) {
			 * 
			 * float dxb = element.getCenterX() - point.getxPos(); float dyb =
			 * element.getCenterY() - point.getyPos(); float propX =
			 * (Math.abs(dxb) / (Math.abs(dyb) + Math .abs(dxb))); float propY =
			 * 1 - propX;
			 * 
			 * return new Point(propX * Math.signum(dyb), propY
			 * Math.signum(dxb)); }
			 * 
			 * Point point2; if (i != aristasReal.size() - 1) { point2 =
			 * aristasReal.elementAt(i + 1); } else { point2 =
			 * aristasReal.elementAt(0); }
			 * 
			 * double distancia = Motor.circleLineDistance(element, point,
			 * point2);
			 * 
			 * if (Math.signum(element.getCenterX() + element.getRadius() -
			 * point.getxPos()) Math.signum(element.getCenterX() -
			 * element.getRadius() - point2.getxPos()) < 0) {
			 * 
			 * if (distancia <= 0) {
			 * 
			 * float dxb = point.getxPos() - point2.getxPos(); float dyb =
			 * point.getyPos() - point2.getyPos(); float propX = (Math.abs(dyb)
			 * / (Math.abs(dxb) + Math .abs(dyb))); float propY = 1 - propX;
			 * return new Point(propX * Math.signum(-dyb), propY
			 * Math.signum(dxb)); } }
			 * 
			 * }
			 */
			return null;
		} else {
			for (Point point : element.getAristas()) {
				if (collition(origin, point)) {
					System.out.println("colision");
					return (new Point(3, 3));
				}
			}
			return null;
		}
	}
}
